<template>
  <!-- 页面容器 -->
  <el-container>
    <!-- 头部区域 -->
    <el-header>
      <!-- 左侧logo部分 -->
      <img src="../assets/images/logo.png" alt />
      <!-- 右侧退出按钮不部分-->
      <el-button type="info" plain @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 左侧导航栏 -->
      <el-aside :width="isCollapse?'65px':'200px'">
        <el-menu
          background-color="#35495E"
          text-color="#fff"
          active-text-color="#41b883"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :default-active="currentLink"
          router
        >
          <div class="simp" @click="simp">
            <i class="el-icon-s-fold"></i>
          </div>
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="item in menus" :key="item.id">
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveCurrentLink('/'+subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容部分 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menus: [],
      iconObj: {
        '125': 'iconfont icon-users',
        '103': 'iconfont icon-lock_fill',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      isCollapse: false,
      currentLink: ''
    }
  },
  methods: {
    logout() {
      // 删除令牌
      sessionStorage.clear('token')
      // 重定向到登录页面
      this.$router.push('/login')
    },
    async getMenuList() {
      // 获取导航菜单列表
      const res = await this.$http.get('menus')
      this.menus = res.data
    },
    simp() {
      this.isCollapse = !this.isCollapse
    },
    saveCurrentLink(currentLink) {
      sessionStorage.setItem('link', currentLink)
    }
  },
  created() {
    this.getMenuList()
    this.currentLink = sessionStorage.getItem('link')
  }
}
</script>

<style lang="less" scope>
.el-header {
  background-color: #41b883;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.el-aside {
  background-color: #35495e;
  .el-menu {
    border-right: none;
  }
  .el-submenu {
    i {
      margin-right: 10px;
      font-size: 20px;
    }
  }
  .simp {
    text-align: center;
    font-size: 20px;
    color: #ffff;
  }
}
</style>